﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SpaceNeedle.x6473.WindowsPhone.ViewModels;
using SpaceNeedle.x6473.WindowsPhone.Common;
using Windows.Networking.Connectivity;
using Microsoft.Phone.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using SpaceNeedle.SDK.WindowsPhone.Views;
using System.Reflection;
using SpaceNeedle.x6473.WindowsPhone.Models;

namespace SpaceNeedle.x6473.WindowsPhone.Views
{
    public class AboutView : ViewBase<AboutViewModel> { }

    public partial class About : AboutView
    {
        public About()
        {
            InitializeComponent();

            this.Loaded += About_Loaded;
        }

        void About_Loaded(object sender, RoutedEventArgs e)
        {
            this.versionTextBlock.Text = this.GetVersion();
        }

        private void share_Click(object sender, EventArgs e)
        {
            string uri = AppViewModel.Instance.Properties.AppContent.About.AboutShareButtonUri;
                        
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.LinkUri = new Uri(uri, UriKind.Absolute);
            shareLinkTask.Show();
        }
    
        private void OnUploadsMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.UploadsCommand.Execute(null);      
        }

        private void OnSignOutMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.SignOutCommand.Execute(null);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!this.ViewModel.IsConnected)
            {
                MessageBox.Show("Você precisa estar conectado à Internet para limpar o cache. Por favor, verifique a sua conexão com a Internet e tente novamente.");
                return;
            }

            this.ViewModel.ClearCacheCommand.Execute(null);
        }

        public string GetVersion()
        {
            var versionAttribute = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).FirstOrDefault() as AssemblyFileVersionAttribute;

            if (versionAttribute != null)
            {
                return versionAttribute.Version;
            }

            return "";
        }

        private void OnCheckUpdateButton_Click(object sender, RoutedEventArgs e)
        {
            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();

            marketplaceDetailTask.ContentIdentifier = Properties.AppId;
            marketplaceDetailTask.ContentType = MarketplaceContentType.Applications;

            marketplaceDetailTask.Show();
        }

    }
}